welcome to another module this time we're going to learn seaborn this module will be our third module for data visualization with the pandas with the matplotlib and with this seaborn you will have sufficient tools to do the project you like in Python and if you would like to explore data visualization in a more in depth way then you may also go ahead and explore Tableau and PowerBI and which are different than the python environment but you can also get fantastic result from those tools and we are going to concentrate in data visualization tools in python okay so again the first step is always do the setup as we have already done many times you can see we are going to import numpy as np, import the pandas as pd and actually seaborn is built upon matplotlab so we are going to import matplotlib.pyplot as plt as well as import seaborn as sns so now we have learned all these nicknames for numpy pandas matplotlib as well as seaborn again it is just some convention and we just use this short name as sns for seaborn in a community you don't have to but if you want to communicate with others well then you the better to follow the convention and we also will use a dummy dataset or very simple dataset directly from seaborn package because seaborn package has some built in datasets and each of them can be loaded use the function in seaborn package we just call load_dataset() and then we specify which data set we are going to use you are going to have the link to all dataset in seaborn and you are able to choose any dataset you like to play with in seaborn's built in dataset such as the titanic which will be used a lot in our data analysis specialization also the iris which is about the flowers also a cute animal called penguins etc so i will leave that for you to explore ration we are going to load this data set as a data frame this time we are going to make a more meaningful name for that data frame called tips okay so we can use our tips.head() to print out first five rows and see what our data is actually looks like we can see we have the total bill which is a column to record the entire amount of the spending in that meal and we also have the tip amount for that total bill and those two are directly related to the interest of the data set which is about tip and for the customer's information we do have some gender smoker and that will be the customer's certain characteristics and we don't know whether or not they will be useful but for this dataset we just record them the other ones, day time and also as size is some characteristics of the meal that is what is the time and what is the size of that meal so we believe maybe this dataset can contribute to an analysis such as certain customers will tip better than other certain customers or certain time the tip will be having a larger or higher percentage than other time we don't know at this moment that is why we are going to use this data set for a simple data visualization in order to find some possible insights and then we can go ahead and do preprocessing and then do data analysis you may not be satisfied with just five rows from the tips how about we print everything out so at least we can see here we have only 244 rows with 7 columns which means that this data set is really really small because 200 rows and just 200 records that is a very small and simple dataset yes we just use this one as a demonstration this is a dummy dataset or toy dataset okay so this will be the understanding of our data if you want to know more about what is the data type of the columns like total bill and the tip for sure they will be in float for six for smoker for day and time they will be like in string or objects and they are more likely to be categorical is because we can see we have very limited labels for them for the size it itself as an integer but we can either make it a categorical if we have only limited size  available for that restaurant or if in some other case the size can be infinite then that will be a numeric so the size depends on what we can see later on so that will be a basic understanding of our data set we have done that many times in data understanding part statistically and now we are going to examine it in terms of visualization by the way seaborn is my personal favorite data visualization too i love seaborn much better than matplotlib it's not because matplotlib is not as good as seaborn the only reason is matplotlib need a lot of customization in order to show a perfect plot seaborn in contradiction will be already perfect if you just use the default ones because seaborn is just so easy and straightforward let me continue for seaborn we are going to introduce this following kind of plots and we are going to introduce you relational plots which is about how attributes are related to each other we also are going to learn how to do distribution plots to learn how one distribution or even more distribution are distributed in terms of their values and for the categorical class we are going to learn something maybe different than what we have learned in matplotlib and in pandas of course cborn provides multiplot functionality so we are going to learn a little bit of the joint plots as well as pair plots just one extra information is that these plots we do in this tutorial are really fundamental and simple there are a lot of specific plots we will not cover in this tutorial because of the time limits however you can always go and check out the documentation of seaborn especially the user guide or tutorial in seaborn and then you can learn much more detail than this introductory tutorial and also this tutorial is based on the user guide of seaborn so i have to give credit to that wonderful package and fantastic developers okay so let us continue with the relational plots as we have already learned in pandas and in matplotlab for the relational plots we most of time want to know how one attribute is related to the other attribute and we can see that the default one for seaborn relational plots as x equal to total bill and y is tip which is very simple is because this function call need three important keywords arguments the first one is data and then we just need to know what is your xs and what is your ys that is quite straightforward and quite simple right once we feed these three critical arguments into seaborn relation plot you will see that automatically we have many elements built in in the result for example the x label which we have to customize it in matplotlib but here the total bill is exactly the name of the column in which you specify your x the same applies to the y label the name of the column you specified as the y will be automatically showing up here as the y label that is quite convenient because most of time if you are if you are a professional data scientist then you're supposed to name your columns in a meaningful way such as total bill and tip of course you have the flexibility to customize the total bill and tip to any label you like, as you did in matplotlib however that will just something extra you can learn and something extra you can apply we are not going to cover them here but don't be limited in this automatically conversion of the name of the column and the label of the axis you are going to use the other one is we can see that seaborn will use the default relational plot if you give two attributes into it as the scatter plot we know it is scatter is because we project these two attributes as a data point in this 2D space and we can see from this graph or from this plot the total bill actually has a kind of positive relationship with the tip which makes sense because as i said the tip most of time will be a percentage of the total bill no matter how small or how large the percentage can be we can see that we see a almost line over here and we can see some variations some people even they have around fortyof the total bill only does around three as the tip which is quite small to be honest the other very useful feature i really like seaborn is this one called hue because a lot of times we just want to make a scatter to know the relationship between two attributes and we just want to make further comparison in this case that data is the same x is the same and y is also the same we just add this extra argument hue equal to sex adding this hue equal to sex will simply make our scatter that have points have different color based on the column you fit here as the hue in other words you are going to make a comparison in terms of color in the same plot use the same type of dots but now you have two different color and you automatically have a nice legend right here so you know that the blue dots are representing male customer while the female dots representing another type of customer so you know this does representing a order placed by male customer while this one placed by a female customer and you can immediately see that even though we have a lot of data messed up in this low and more common area we do have the large amount of total bill as well as large amount of tip from male customers so this hue i really like it is because adding this one extra argument you can add so many vivid information comparison differentiation and a lot of insights from this plot how about we do some other relationship such as the total bill and this is our x and this is y which is all the same but let's hue another column let's call the day now this one will be more interesting because we learned that hue supposed to give you multiple colors for you to do a meaningful comparison so we normally will here with a categorical column and the categorical column may be having 2 3 4 or more labels and we have observed that with two labels, sex, can provide us a very useful plot for comparison and these four labels in the day even though we have only thursday friday saturday and sunday we can only see some of the information but sometimes it just feels overwhelming if we have four attributes so think about if you are going to hue on a column that has more than 5 or even 10 or even 20 then basically you will be short of color or you cannot have significant comparison among that many colors so four might be around that edge already even though we can see that saturday is the lucky day for the restaurant we have the total bill all happens in the saturday and we have the largest at the tip on that saturday as well and we do see some thursdays or around this area and also friday etc so that might be the reason that people will gather together in a weekend and have a larger bill that might be so we don't know at a this moment but we can get some insights from this comparison by adding this hue equal to day another one let's just hue one more, how about the time for the time we actually have only lunch and dinner as two possible time and this is a much better comparison because we cannot be that busy compare four or five or more things at the same time so we can see that the dinner again the largest amount of the total bill happens in the dinner time and also the tip and we can see the lunch time is actually has a much more condensed area in a lower total bill and a lower tip because people just get busy in the lounge and need to quickly go back to work We have now explored how to simply do a relational plot showing as a scatter add the data x and y and also add the hue which is a categorical value for comparison in terms of color however we do something a little bit more now instead of just to have the data x and y and hue we can add another one as col the column is similar to the multi plot we learned in matplotlib so rather than have everything in one plot the column basically will automatically create multiple plots use the column as you specify it here so as we know the time has two possible value so we have lunch value as one plot and a dinner value as another plot within each of the sub plots we can still do the x and y and the hue that will be quite straightforward and easy another advantage is that to save the space we have only one legend not two which will be quite convenient because you don't have to specify this is the plot i want legend and this is the plot i don't have to also for the y you can see that to save space and the make of the plot simple we have only one y label as tip at this place for here because we don't necessarily have to repeat the same wide label twice so we just automatically eliminate it and for the time equal to lunch and time equal to dinner it is automatically added as well so that we don't have to worry about that part basically adding this one extra argument you are going to get a lot of task automatically done for you i hope you are happy with that i really like this kind of feature okay so this will be the bad example i mentioned using the same x and y if we heal with something has a lot of labels like the size we actually not that many but still 1 to 6 a lot of things are happening and it may not be that meaningful so be careful when you use the hue and just don't apply the view to a categorical value with too many labels and for the size we actually can do something with the size that is the size argument so we learned in matplotlib we can make our dots in different sizes if we want to show the differences in a third column or third dimension for that data point we can do similar things it is in seaborn as well we can simply assign the size as the size and also hue the size so you will have a dots with different size as well as different color automatically the size will be like the color bar we learned in the matplotlib that the smaller it might be lighter and in the more we have the darker etc and we can also get the sense of the size if this size from one to six is too similar to you and we can also specify the size from the minimum to the maximum as minimum 15 maximum as 200 to make an even larger contradiction so this is a modified size our sides has a column from the size column and our sizes defines the property or the attributes of the size in order to show the size of your thoughts so in this case we can see that the 6 has a much larger dots than the one as the one as the one as the two so on and so forth so this will make a much more straightforward delivery to your audience if you take this example now we have already learned the alpha as in matplotlib to reduce overlapping or transparency we can do the same thing in seaborn as well so you can see here our alpha is 0.5 which means that we are going to show some of the overlapping with 50% of the transparency okay so we have learned a lot using relational plot using x as total bill and y as a tip how about we do some other thing like how about we fill the x's with a categorical value we know it is just the four values and the y with the total bill for this one you will see okay it is oh not that meaningful because we just have some limited xs which makes out y is like a bar and more like a distribution along the y axis in this way we can actually call the relational plots and use the other two attributes as relationship but now we can specify the kind as a line this line will basically remove the data points for that particular observation of the day but show you the critical interval and the meaning of that particular day so now from this plot we can see there is a blue line that basically what is the mean value for all the total bill on thursday and what is the mean value for the total bill on sunday and clearly sunday has a much higher meaning for the total bill for the light blue area around this line is actually the 95% critical interval shows that what is the typical value you can see on that day so of course friday has the largest of variance and the sunday and the saturday has a much smaller variance and we can see their most common are the total bill are higher than 18 that is the case and then we can do the hue for this kind of line relational plots as well okay so we can see that in this plot we can show the male customer and the female customer they have a different meaning of the total bill and also they have a different distribution or critical interval of their total bill so that might be a better comparison in terms of how you want to show just for the day what will be the total bill looks like and again we can apply the column as well so we see that the lunch is only provided on thursday and friday and the dinner and only provided on friday saturday and sunday that might be the strategy in that restaurant and we can show the different line plots for the relationship as well so the relational plot as rel plot as we explored here again are quite simple because we follow just rel plot and fill in the data x and y and we have very limited arguments to play with in these very simple graphs that is the hue to make comparison kind to specify the type of plots and then column if you want to have subplots showing up here but this combination or the combination of x and y can actually yields really really good result so i encourage you to try with other attributes other columns for the relational plot okay so let's continue with our distribution plot as we learn the distribution plot is always try to know how your data in terms of a frequency or rare or normal or quantile how they are looking like so of course the distribution plot will be like a histogram as we learned in matplotlab however again you just need to show that this plot displot with the data and with the column you want then the histogram will automatically showing up to you nicely with the total bill as the x label and count as the y level so clearly we see that if this is let's say the value larger than ten and the less than 13 we have the count of 45 and then the most typical value will be around 15 dollars and then that we have around 50 counts so that is straightforward and informative in terms of all the labels etc all we need to do is to call the distribution plot feed the data feed the xs that's all so simple okay we learned we can do the column so how about we separate the male customer and the female customer as the subplot and again we can see the subplot title is nicely written here and the count is automatically eliminated for the second plot and we can see the two different subplots do have a little bit different distribution and yeah that will be the difference between the male customer and the female customer if we want to compare them in the same plot we can do the hue in the hue as you can see that they are having some overlapping clearly which is the gray area and the further things they are not overlapping such as this one and this one particularly for the female customer and for this one this one and this one particularly for the male customer we can also see them so in this side by side comparison you can even see clearly how they are similar and different we can combine the hue and the column in the same graph and now because our column is the day as we know we have four different days now the distribution for your total bill and you are making a comparison on sex and then you are separating them on different days all showing up here as you can see again quite easy calling for that arguments and quite informative plots just done by one line of code and you have nicely the columns name as the subplot as well as the legend as well as the count and then we can make something different rather than the histogram we learned that for the distribution we can actually use the density curve that is kde for that one we can do so for our distribution plus as well you seem to specify the kind as a kde then you will see that the female customer and the male customer are overlapping but then in the tail part it is quite different and then if you don't want them to be overlapping you can specify your multiple is stack so still same data same x and the same hill and the same kind you just add this multiple equal to stack in the multiple equal to stack you can compare with the previous one you are going to remove the overlapping part but stacking them so that if you discard the hue this will be the entire distribution for the total bill regardless of the sex if you want to know more detail of each portion of the sex in terms of the total bill distribution then you can look into the color inside of that distribution quite convenient right you have two birds by one stone and then we can do this kind of column as the day again for the comparison of the sex and then stack them as the multiple so you see they are having different shapes of distribution not just for the total bill but also in terms of different gender the other advantage is that for the distribution plot as we learned in pandas and in matplotlab we can only do with one column and if you want to do with two column that will need a lot of customization however for our seaborn if you want to do two column distribution you just need to fill two columns in which is your x and y so for here we have the total bill and the tip and then we just feed them in as you can see that the distribution as the summits of your most frequent value of the total bill as well as your tip will be around in this area so that will be the total bill and a tip and the kind is kde because we do want to have the distribution in terms of the curve and we can view or compare different columns as well in this case we can see the blue ones is for male customer and the yellow ones is for the female customer and this is to show you how we can add another important and useful information together in our distribution plot with the two variables total bill and the tip so this will be a normal total bill and tip as two variable distribution plot kde the extra information we embedded here which is rug = True so we can basically visually see how your data frequently distributed and here is the dense area and here is the sparse area and here is another dense area which is two dense areas contributes to the summit of this mountain of distribution and if we want to know how your data is go from the least value to the highest value proportionally you can call the distribution plot with the kind of ecdf and then you can see that you total bill go from the minimum to the maximum in this kind of incremental curve of course we can compare the gender as well and we can even compare the gender and make them subplots with different columns as well as compare how they accumulate from the least to the greatest how they involve etc that will be our distribution plots and let's continue with our categorical plots and we learn the categorical plots will try to make some meaningful visualization for a attribute has limited or finite or few labels let's see if we just do a categorical plot with the data as tips x as day and y as total bill as we can see it is similar to the scatter plot but now it is not just a straight line but some of the distribution of your data points which we know it is true but not that meaningful how about we make it a little bit comparable with the hue by the sex then we can see okay they are messed up they are colorful but still it cannot provide too much information in this case and how about we use our box we learned the box plot can show you a lot of data distribution information such as center tendency as well as dispersion as well as your outliers and we can see that here for the categorical plot we can show them nicely in terms of comparison in colors so the thursday has the measure of the data in this area and friday and saturday and sunday and we have some of our relations which is the theoretical upper limit and the lower limit we have some outliers in terms of customers consumed more than normal and then that will be our result from this box product and we can make the box plot as a comparison not just in terms of the day but also in terms of the sex so we can see the male customer and the female customer has a very different distribution especially on thursdays and fridays and we can show them a little bit different use a violin for the violin we learned in matplotlab already and that just basically show the distribution as well so we can see the distribution for the categorical plot and for here the violin basically is the same but we can see from inside we can see the median quantiles and theoretical limit of your normal data and your actual data distribution from your minto your max so this violin represents much more information than the one in matplotlib by default and you can see also our calling for the categorical plot is very general that is call catplot, we selected data x and y and then to specify your kind is a violin and for the violin of course we can do the hue to make a comparison with the sex and yeah that will be the result sometimes you feel  the violin or the box are wasting your space because we always try to feed as much information as possible in the limited space and because the violin and the box are they are actually symmetric to this center line so how about we do something like this if we have the split equal to True basically we are going to merge these two together and get rid of this half because this half is symmetric to this one this half is the same as this one so how about we get rid of that part and merge the two halves together as a violin so this again will just show your customer a little bit different but the same information and if we want to add explicitly the quantiles that is q1 q2 q3 and those kind of information you can do so but then as we learned in matplotlib the specific arguments will have to be called in a specific function you have to call from violinplot that is a specified plot then you can specify the inner quantile like this one showing the bars of the quantiles and you can specify the kind as bar as well for the bar we can see the different colors for the categories and also some of the distribution of your value with some comparison with the gender as well so as we can see here relational plot distribution of plot and categorical plot they are the three major plots for your attributes and now we are going to see another two the first one is joint plot that you have the opportunity to have the major plot showing in this center area and have some secondary plot showing in the side of your plot so in this place we are going to show that this is the two variable kde distribution and for here we see one variable distribution and we make a difference between the male customer and the female customer so this single plot invades a lot of information and if you don't like the distribution plot you can make it a scatter in here and then you can still show the distribution on the side of your plot and you can also try use the scatter but now use the distribution as histogram rather than as the kde and you can also show a different plus by specifying your different initial majority of the plus like this one and then this one as well as all the other possible choices i will leave them for you to explore and because they are just representing different scenarios for you to try all the arguments you need to learn how to try them is actually in this kind so this kind k is going as a for loop for the scatter hist hex kde and the reg and resid the reg could give you a possible regression line as we can see here the residual give you a possible error term around the predicted line so those things we are going to explore more in data analysis but now you already have a basically understanding of possible relationship between the attributes that is our joint plot the last one is pair plot which is the one that will cause a lot of resources at a time so for this one we basically call from the data without specifying the columns so basically this pair plot will do pair wise attributes matching and will create a plot for each pair of possible plots because we have in our dataset we have only total bill tip and sides they are numeric attributes so we are seeing that a 3 by 3 pair plot however if you have a dataset has a lot of numeric attributes then you will get a much larger matrix of the pair plot and of course it's quite easy because you just call the pair plot and you get a lot of subplots together at the same time but it will also cost a lot of time and resources and in the pair plot you can also specify the comparison which is the hue as the sex and then you can see how they are different and the one thing in particular useful and convenient is that automatically for the same attribute which is the diagonal that is the total bill to total bill because this is just the one attribute automatically there will be the distribution for the other ones they will be relational okay so i hope you find this seaborn tutorial is useful again check out the documentation with the seaborn and learn more from the user tutorial and for the examples on the package